package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.constant.MessageConstant;
import com.sky.dto.*;
import com.sky.entity.People;
import com.sky.exception.AddressBookBusinessException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.PeopleMapper;
import com.sky.result.PageResult;
import com.sky.service.PeopleService;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.security.NoSuchAlgorithmException;
import java.util.List;

@Service
public class PeopleServiceImpl implements PeopleService {
    @Autowired
    private PeopleMapper peopleMapper;

    /**
     * 登录
     * @param peopleLoginDTO
     * @return
     */
    @Override
    public People login(PeopleLoginDTO peopleLoginDTO) {
        String phone=peopleLoginDTO.getPhone();
        String password=peopleLoginDTO.getPassword();
        //根据用户名查询数据库中的数据
        LambdaQueryWrapper<People> lam=new LambdaQueryWrapper<>();
        lam.eq(People::getPhone,phone);
        People people=peopleMapper.selectOne(lam);
        //处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if(people==null){
            //账号不存在
            throw new AddressBookBusinessException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //密码对比
        password= DigestUtils.md5DigestAsHex(password.getBytes());
        if(!password.equals(people.getPassword())){
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        //返回实体对象
        return people;
    }

    /**
     * 用户注册
     * @param peopleDTO
     */
    @Override
    public void save(PeopleDTO peopleDTO) {
        //创建对象
        People people=new People();
        //对象属性拷贝（phone,password）
        BeanUtils.copyProperties(peopleDTO,people);
        //设置密码
        String password=peopleDTO.getPassword();
        people.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        peopleMapper.insert(people);
    }

    /**
     * 用户修改信息
     * @param peopleDTO
     */
    @Override
    public void update(PeopleDTO peopleDTO) {
        People people=new People();
        BeanUtils.copyProperties(peopleDTO,people);
        //修改的密码加密
//        String password=peopleDTO.getPassword();
//        people.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        peopleMapper.updateById(people);
    }

    /**
     * 根据ID查询用户信息
     * @param id
     * @return
     */
    @Override
    public People selectOne(Long id) {
        People people = peopleMapper.selectById(id);
        return people;
    }

    @Override
    public int selectByPhone(String phone) {
        LambdaQueryWrapper<People> lam=new LambdaQueryWrapper<>();
        lam.eq(People::getPhone,phone);
        People people = peopleMapper.selectOne(lam);
        if(people==null){
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    public Long selectCount() {
        LambdaQueryWrapper<People> lam=new LambdaQueryWrapper<>();
        Integer i = peopleMapper.selectCount(lam);
        Long count= Long.valueOf(i);
        return count;
    }

    @Override
    public PageResult pageQuery(PeoplePageQueryDTO peoplePageQueryDTO) {
        IPage iPage=new Page(peoplePageQueryDTO.getPage(),peoplePageQueryDTO.getPageSize());
        LambdaQueryWrapper<People> lam=new LambdaQueryWrapper<>();
        if(peoplePageQueryDTO.getName()!=null&&peoplePageQueryDTO.getName()!=""){
            lam.like(People::getName,peoplePageQueryDTO.getName());
        }
        if(peoplePageQueryDTO.getPhone()!=null&&peoplePageQueryDTO.getPhone()!=""){
            lam.like(People::getPhone,peoplePageQueryDTO.getPhone());
        }
        peopleMapper.selectPage(iPage,lam);
        long total=iPage.getTotal();
        List<People> peopleList = iPage.getRecords();
        PageResult pageResult=new PageResult(total,peopleList);
        return pageResult;
    }

    @Override
    public void deleteUser(Long id) {
        peopleMapper.deleteById(id);
    }

    @Override
    public Boolean updatePassword(UpdateUserPassword updateUserPassword) {
        LambdaQueryWrapper<People> lam=new LambdaQueryWrapper<>();
        lam.eq(People::getPhone,updateUserPassword.getPhone());
        //根据ID查询用户信息
        People people = peopleMapper.selectOne(lam);
        //获取数据库中的密码
        String password = people.getPassword();
        //获取前端传来的密码
        String oldPassword = updateUserPassword.getPassword();
        //加密
        oldPassword = DigestUtils.md5DigestAsHex(oldPassword.getBytes());
        //对比
        if (password.equals(oldPassword)) {
            return false;
        }
        //修改密码
        String newPassword = updateUserPassword.getPassword();
        newPassword = DigestUtils.md5DigestAsHex(newPassword.getBytes());
        people.setPassword(newPassword);
        peopleMapper.updateById(people);
        return true;
    }



}
